<?php

namespace app\admin\controller;
use think\Db;
use app\common\service\BalanceService;
class System extends Common{

	public function index(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function dataList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_system_param')
				->where('is_show',1)
				->where('type',1)
				->order('id asc')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}
	/*
	编辑参数
	 */
	public function editVal(){
		if (request()->method() == 'POST') {
			$id = request()->param('id');
			$val = request()->param('val');
			$res  = Db::table('tp_system_param')->where('id',$id)->update(['val'=>$val]);

			if ($id == 254) {
				Db::table('tp_coin')->where('id',10)->update(['price'=>$val]);
				Db::table('tp_coin_wallet')->where('id',10)->update(['price'=>$val]);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}
		}		
	}

	public function button(){
		return $this->fetch();
	}

	/*
	数据接口
	*/
	public function buttonList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_system_param')
				->where('is_show',1)
				->where('type',2)
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}

	/*
	编辑开关
	 */
	public function editButton(){
		if (request()->method() == 'POST') {
			$id = request()->param('id');
			$info = Db::table('tp_system_param')->where('id',$id)->find();
			if($info['val'] == 0){
				$res  = Db::table('tp_system_param')->where('id',$id)->update(['val'=>1]);
			} else {
				$res  = Db::table('tp_system_param')->where('id',$id)->update(['val'=>0]);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}
		}		
	}

	/*
	轮播图
	 */
	public function slideshow(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function slideshowList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_slideshow')
				->paginate($limit);
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['pic'] = $this->imageUrl($val['pic']);
		}
		return $this->tableData($data);
	}
	/*
	删除轮播图
	 */
	public function delSlideshow(){
		$id = request()->param('id');
		$res = Db::table('tp_slideshow')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'删除失败'];
		}
	}
	/*
	新增轮播图
	 */
	public function addSlideshow(){
		if (request()->method() == 'POST') {
			$data = [];
			$pic  = request()->param('pic');
			$data['pic'] = $pic;
			$data['status'] = 1;
			$res  = Db::table('tp_slideshow')->insert($data);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			return $this->fetch();
		}
	}


	/*
	轮播图
	 */
	public function question(){
		return $this->fetch();
	}
	/*
	数据接口
	*/
	public function questionList(){
		$limit = request()->param('limit')?:20;
		$data  = Db::table('tp_question')
				->paginate($limit);
		$data = $data->toArray();
		return $this->tableData($data);
	}
	/*
	删除轮播图
	 */
	public function delQuestion(){
		$id = request()->param('id');
		$res = Db::table('tp_question')->where('id',$id)->delete();
		if ($res) {
			return ['code'=>0,'msg'=>'successs'];
		} else {
			return ['code'=>1,'msg'=>'删除失败'];
		}
	}
	/*
	
	 */
	public function editQuestion(){
		if (request()->method() == 'POST') {
			$data = [];
			$title  = request()->param('title');
			$content  = request()->param('content');
			$data = [];
			$data['title'] = $title;
			$data['content'] = $content;
			$id  = request()->param('id');
			if($id){
				$res = Db::table('tp_question')->where('id',$id)->update($data);
			} else {
				$res  = Db::table('tp_question')->insert($data);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id  = request()->param('id');
			$data = [];
			if($id){
				$data = Db::table('tp_question')->where('id',$id)->find();
			}
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	/*
	注册赠送
	 */
	public function reg(){
		if (request()->method() == 'POST') {
			$data = [];
			$data['is_open'] = request()->param('is_open');
			$data['cid'] = request()->param('cid');
			$data['amount'] = request()->param('amount');
			$id  = request()->param('id');
			if($data['is_open'] == 'on'){
				$data['is_open'] = 1;
			} else {
				$data['is_open'] = 0;
			}
			if($id){
				$res = Db::table('tp_reg_config')->where('id',$id)->update($data);
			} else {
				$res  = Db::table('tp_reg_config')->insert($data);
			}
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$id  = request()->param('id')?:1;
			$data = [];
			if($id){
				$data = Db::table('tp_reg_config')->where('id',$id)->find();
			}
			$coin = Db::table('tp_coin')->where('status',1)->field('id,name')->select();
			$this->assign('coin',$coin);
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	/*
	客服二维码
	 */
	public function kf(){
		if (request()->method() == 'POST') {
			$pic1 = request()->param('pic1');
			$pic2 = request()->param('pic2');
			Db::table('tp_system_param')->where('param','kf_code1')->update(['value'=>$pic1]);
			$res = Db::table('tp_system_param')->where('param','kf_code2')->update(['value'=>$pic2]);
			if ($res) {
				return ['code'=>0,'msg'=>'操作成功'];
			} else {
				return ['code'=>1,'msg'=>'操作失败'];
			}			
		} else {
			$pic1 = C('kf_code1');
			$pic2 = C('kf_code2');
			$this->assign('pic1',$pic1);
			$this->assign('pic2',$pic2);
			return $this->fetch();
		}
	}


	/*
	隐私声明
	 */
	public function pravicy(){
		if (request()->method() == 'POST') {
			//$data['title'] = request()->param('title');
			//$data['time'] = request()->param('time');
			$content = input('post.content','',null);
			//return $data['content'];
			$res = Db::table('tp_system_param')->where('param','ys_xy')->update(['val'=>$content]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$pravicy = Db::table('tp_system_param')->where('param','ys_xy')->value('val');
			$this->assign('pravicy',$pravicy);
			return $this->fetch();
		}
	}

	/*
	关于我们
	 */
	public function aboutUs(){
		if (request()->method() == 'POST') {
			//$data['title'] = request()->param('title');
			//$data['time'] = request()->param('time');
			$content = input('post.content','',null);
			//return $data['content'];
			$res = Db::table('tp_system_param')->where('param','about_us')->update(['val'=>$content]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$pravicy = Db::table('tp_system_param')->where('param','about_us')->value('val');
			$this->assign('pravicy',$pravicy);
			return $this->fetch();
		}
	}
	/*
	帮助中心
	 */
	public function helpCenter(){
		if (request()->method() == 'POST') {
			//$data['title'] = request()->param('title');
			//$data['time'] = request()->param('time');
			$content = input('post.content','',null);
			//return $data['content'];
			$res = Db::table('tp_system_param')->where('param','help_center')->update(['val'=>$content]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$pravicy = Db::table('tp_system_param')->where('param','help_center')->value('val');
			$this->assign('pravicy',$pravicy);
			return $this->fetch();
		}
	}
	/*
	注册我们
	 */
	public function regXy(){
		if (request()->method() == 'POST') {
			//$data['title'] = request()->param('title');
			//$data['time'] = request()->param('time');
			$content = input('post.content','',null);
			//return $data['content'];
			$res = Db::table('tp_system_param')->where('param','reg_xy')->update(['val'=>$content]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$pravicy = Db::table('tp_system_param')->where('param','reg_xy')->value('val');
			$this->assign('pravicy',$pravicy);
			return $this->fetch();
		}
	}
		/*
	服务我们
	 */
	public function serviceXy(){
		if (request()->method() == 'POST') {
			//$data['title'] = request()->param('title');
			//$data['time'] = request()->param('time');
			$content = input('post.content','',null);
			//return $data['content'];
			$res = Db::table('tp_system_param')->where('param','service_xy')->update(['val'=>$content]);
			if ($res) {
				return ['code'=>0,'msg'=>'success'];
			} else {
				return ['code'=>1,'msg'=>'更新失败'];
			}			
		} else {
			$pravicy = Db::table('tp_system_param')->where('param','service_xy')->value('val');
			$this->assign('pravicy',$pravicy);
			return $this->fetch();
		}
	}
}